package com.zx.centers.socketservice.server;

import com.zx.centers.socketservice.dto.IMMessage;
import com.zx.centers.socketservice.dto.IMUser;
import com.zx.centers.socketservice.user.OnlineUser;
import com.zx.centers.socketservice.user.TUser;
import com.zx.centers.socketservice.user.UserCache;
import com.zx.centers.socketservice.util.ChatUtil;
import com.zx.centers.socketservice.util.contants.ChatContants;
import io.netty.channel.ChannelHandler;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@ChannelHandler.Sharable
public class ServerRpcHandler extends SimpleChannelInboundHandler<IMMessage> {

    @Autowired
    private OnlineUser onlineUser;

    private Logger logger = LoggerFactory.getLogger(ServerRpcHandler.class);

    @Override
    protected void channelRead0(ChannelHandlerContext channelHandlerContext, IMMessage msg) throws Exception {
        if(ChatContants.MSGTYPE_LOGIN.equals(msg.getMsgType())){
            if(UserCache.getUser(msg.getSender().getUid())==null){
                msg.setResult(false);
                msg.setMsg("用户名不存在");
                channelHandlerContext.writeAndFlush(msg);
            }else{
                TUser user = UserCache.getUser(msg.getSender().getUid());
                if(msg.getLoginPwd().equals(user.getLoginPwd())){
                    msg.setMsg("登录成功");
                    msg.setResult(true);
                    TUser tUser = UserCache.getUser(msg.getSender().getUid());
                    msg.getSender().setUname(tUser.getUname());
                    msg.getSender().setUsex(tUser.getUsex());
                    msg.setFriends(ChatUtil.convertUsers(user.getFriends()));
                    onlineUser.put(msg.getSender().getUid(), channelHandlerContext);
                }else{
                    msg.setResult(false);
                    msg.setMsg("登录失败密码错误");
                }
                channelHandlerContext.writeAndFlush(msg);
            }
        }else{
            ChannelHandlerContext uChc = onlineUser.get(msg.getSender().getUid());
            if(uChc==null){
                onlineUser.put(msg.getSender().getUid(), channelHandlerContext);
            }

            ChannelHandlerContext rChc = onlineUser.get(msg.getReceiver().getUid());
            if(rChc==null){
                IMUser sender = msg.getSender();
                IMUser receiver = msg.getReceiver();
                msg.setSender(receiver);
                msg.setReceiver(sender);
                msg.setMsg("对方不在线！");
                msg.setMsgType(ChatContants.MSGTYPE_OFFINE);
                channelHandlerContext.writeAndFlush(msg);
            } else {
                rChc.writeAndFlush(msg);
            }
        }
    }

    @Override
    public void channelRegistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelRegistered");
        super.channelRegistered(ctx);
    }

    @Override
    public void channelUnregistered(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelUnregistered");
        super.channelUnregistered(ctx);
    }

    @Override
    public void channelActive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelActive");
        super.channelActive(ctx);
    }

    @Override
    public void channelInactive(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelInactive");
        super.channelInactive(ctx);
    }

    @Override
    public void channelReadComplete(ChannelHandlerContext ctx) throws Exception {
        System.out.println("channelReadComplete");
        super.channelReadComplete(ctx);
    }

    @Override
    public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
        onlineUser.remove(ctx);
        System.out.println("exceptionCaught");
        ctx.close();
        super.exceptionCaught(ctx, cause);
    }

    @Override
    public void handlerAdded(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerAdded");
        super.handlerAdded(ctx);
    }

    @Override
    public void handlerRemoved(ChannelHandlerContext ctx) throws Exception {
        System.out.println("handlerRemoved");
        super.handlerRemoved(ctx);
    }
}
